package nsh.cl.nd.khtn.mazedash.game.play;

import nsh.cl.nd.khtn.mazedash.GameConstant;
import nsh.cl.nd.khtn.mazedash.MapManager;
import nsh.cl.nd.khtn.mazedash.RootGameActivity;
import nsh.cl.nd.khtn.mazedash.baseclasses.BaseFadeInScene;
import nsh.cl.nd.khtn.mazedash.entity.CellType;
import nsh.cl.nd.khtn.mazedash.entity.GUICell;

import org.andengine.entity.particle.SpriteParticleSystem;
import org.andengine.entity.particle.emitter.PointParticleEmitter;
import org.andengine.entity.particle.initializer.AccelerationParticleInitializer;
import org.andengine.entity.particle.initializer.BlendFunctionParticleInitializer;
import org.andengine.entity.particle.initializer.ColorParticleInitializer;
import org.andengine.entity.particle.initializer.RotationParticleInitializer;
import org.andengine.entity.particle.initializer.VelocityParticleInitializer;
import org.andengine.entity.particle.modifier.AlphaParticleModifier;
import org.andengine.entity.particle.modifier.ColorParticleModifier;
import org.andengine.entity.particle.modifier.ExpireParticleInitializer;
import org.andengine.entity.particle.modifier.ScaleParticleModifier;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.opengl.GLES20;

public class PlayScene extends BaseFadeInScene {

	private BitmapTextureAtlas directionTextureAtlas;
	private TextureRegion directionTextureRegion;
	private GUICell mainChar;
	
	private MapManager mapManager;
	private Text text;
	private GameOverPopupScene gameOverPopup;
	private SpriteParticleSystem particleSystem;
	
	public PlayScene() {
		super();
	}
	
	@Override
	protected void loadSceneResources() {
		setBackground(new Background(0.0f, 0.0f, 0.0f));
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		
		directionTextureAtlas = new BitmapTextureAtlas(RootGameActivity.getInstance().getTextureManager(), 2048, 2048);
		directionTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(directionTextureAtlas,
				RootGameActivity.getInstance(), "yyy.png", 0, 0);
		directionTextureAtlas.load();

		VertexBufferObjectManager vertexBufferObjectManager = RootGameActivity.getInstance()
				.getVertexBufferObjectManager();
		mainChar = new GUICell(0, 0, directionTextureRegion, vertexBufferObjectManager);
		mainChar.setPosition((GameConstant.CAMERA_WIDTH - mainChar.getWidth()) / 2,
				(GameConstant.CAMERA_HEIGHT - mainChar.getHeight()) / 2 - 250);

		
		setTouchAreaBindingOnActionDownEnabled(true);
		setOnSceneTouchListenerBindingOnActionDownEnabled(true);
		
		RootGameActivity.getInstance().getSwipeDetector().setPlayScene(this);
		setOnSceneTouchListener(RootGameActivity.getInstance().getSwipeDetector());
		
		text = new Text(100, 60, RootGameActivity.getInstance().getScreenManager().getSmallFont(), "", 50000, vertexBufferObjectManager);
		text.setPosition(50, (GameConstant.CAMERA_HEIGHT - mainChar.getHeight()) / 2 - 250);
		GUICell xx = new GUICell(0, 0, directionTextureRegion, vertexBufferObjectManager);
		xx.setCellType(CellType.PATH);
		mainChar.setCellType(CellType.WALL);
		
		gameOverPopup = new GameOverPopupScene();
		gameOverPopup.loadResource();
		mapManager = new MapManager(mainChar);
		
		
		BitmapTextureAtlas mBitmapTextureAtlas = new BitmapTextureAtlas(RootGameActivity.getInstance().getTextureManager(), 32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion mParticleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, RootGameActivity.getInstance(), "particle_square.png", 0, 0);
		mBitmapTextureAtlas.load();
		particleSystem = new SpriteParticleSystem(new PointParticleEmitter(100, 800), 30, 60, 50, mParticleTextureRegion, vertexBufferObjectManager);
		particleSystem.addParticleInitializer(new BlendFunctionParticleInitializer<Sprite>(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE));
		particleSystem.addParticleInitializer(new VelocityParticleInitializer<Sprite>(50, 200, 0, 0));
		particleSystem.addParticleInitializer(new AccelerationParticleInitializer<Sprite>(0, 0));
		particleSystem.addParticleInitializer(new RotationParticleInitializer<Sprite>(0.0f, 360.0f));
		particleSystem.addParticleInitializer(new ColorParticleInitializer<Sprite>(1.0f, 1.0f, 0.0f));
		particleSystem.addParticleInitializer(new ExpireParticleInitializer<Sprite>(2.0f));

		particleSystem.addParticleModifier(new ScaleParticleModifier<Sprite>(0, 5, 0.5f, 2.0f));
		particleSystem.addParticleModifier(new ColorParticleModifier<Sprite>(1.0f, 3.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f));
		particleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(1.0f, 3.0f, 1.0f, 0.0f));
		
		attachChild(particleSystem);
	}

	public void startNewGame() {
		mapManager.loadDataMap(0);
		for (int i = 0; i < mapManager.getGuiMap().length; i++) {
			for (int j = 0; j < mapManager.getGuiMap()[i].length; j++) {
				attachChild(mapManager.getGuiMap()[i][j].getGuiCell());
			}
			
		}
	}

	@Override
	protected void onScreenReady() {
		startNewGame();
	}

	public GUICell getMainChar() {
		return mainChar;
	}

	public Text getText() {
		return text;
	}

	public MapManager getMapManager() {
		return mapManager;
	}

	public void gameOver() {
		gameOverPopup.showPopup(this);
	}

	public SpriteParticleSystem getParticleSystem() {
		return particleSystem;
	}

}